//
//  MyCcsClientMessageDelegate.m
//  IVCS
//
//  Created by nfl on 2022/2/23.
//  Copyright © 2022 zealcomm user. All rights reserved.
//

#import "MyCcsClientMessageDelegate.h"
#import "Tool.h"
@implementation MyCcsClientMessageDelegate


- (void)onCcsServerMessage:(NSArray * _Nonnull)data {
    NSDictionary *message = data.firstObject;
    ZcoModelCcsMessage *ccsMessage = [[ZcoModelCcsMessage alloc] initWithDictionary:message] ;
    SessionMessageType type = ccsMessage.type;
    NSDictionary *dataInfo = ccsMessage.data;
    if (!self.ivcsCustomerDelegate) {
        return;
    }
    if([type isEqualToString:SessionMessageTypePeerCmd]){
        ZcoModelCMD *cmd = [[ZcoModelCMD alloc] initWithDictionary:dataInfo];
        ccsMessage.data = cmd;
        if ([cmd.cmd isEqualToString:PeerCmdTypePhoteGuideBox]) {
            //推送引导框
            if ([self.ivcsCustomerDelegate respondsToSelector:@selector(onPhotoGuideBox:)]) {
                [self.ivcsCustomerDelegate onPhotoGuideBox:ccsMessage];
            }
        }
        else if([cmd.cmd isEqualToString:PeerCmdTypeView]){
            ZcoModelLinkInfo *linkInfo = [[ZcoModelLinkInfo alloc] initWithDictionary:cmd.data];
            cmd.data = linkInfo;
             if ([self.ivcsCustomerDelegate respondsToSelector:@selector(onCmdView:)]) {
                [self.ivcsCustomerDelegate onCmdView:ccsMessage];
            }
        }
        else if([cmd.cmd isEqualToString:PeerCmdTypeScreenSnapshot]) {
            //截图
            if ([self.ivcsCustomerDelegate respondsToSelector:@selector(onScreenSnapshotRequest:)]) {
                [self.ivcsCustomerDelegate onScreenSnapshotRequest:ccsMessage];
            }
        }
        else if([cmd.cmd isEqualToString:PeerCmdTypeGetHandwriting]){
            //手写签名
            if ([self.ivcsCustomerDelegate respondsToSelector:@selector(onHandSignatureRequest:)]) {
                [self.ivcsCustomerDelegate onHandSignatureRequest:ccsMessage];
            }
        }
        else if([cmd.cmd isEqualToString:PeerCmdTypeGetLocation]){
            //获取地理位置
            if ([self.ivcsCustomerDelegate respondsToSelector:@selector(onRequestLocation:)]) {
                [self.ivcsCustomerDelegate onRequestLocation:ccsMessage];
            }
        }
        else if([cmd.cmd isEqualToString:PeerCmdTypeSendForm]){
            // 表单
            ZcoModelFormBrief *formBrief = [[ZcoModelFormBrief alloc] initWithDictionary:cmd.data];
            cmd.data = formBrief;
            if ([self.ivcsCustomerDelegate respondsToSelector:@selector(onFormData:withFormUrl:)]) {
                NSString *url = [NSString stringWithFormat:@"%@/form/form-submit/?platform=IOS&hash=%@&url=%@&id=%ld&formId=%ld&followId=%@&token=%@" , [CCSClient shared].baseUrl , [Tool getCurrentTimeMillions] , [CCSClient shared].baseUrl , (long)formBrief.formTemplate , (long)formBrief.formId , formBrief.followId , [CCSClient shared].accessToken];
                [self.ivcsCustomerDelegate onFormData:ccsMessage withFormUrl:url];
            }
        }
        else if([cmd.cmd isEqualToString:PeerCmdTypeScanCode]){
            // 扫码
            if ([self.ivcsCustomerDelegate respondsToSelector:@selector(onScanCode:withMessage:)]) {
                [self.ivcsCustomerDelegate onScanCode:@YES withMessage:ccsMessage];
            }
        }
    }
    else if ([type isEqualToString:SessionMessageTypePeerCancel]){
        ZcoModelCMD *cmd = [[ZcoModelCMD alloc] initWithDictionary:dataInfo];
        ccsMessage.data = cmd;
        if ([cmd.cmd isEqualToString:PeerCmdTypeScanCode]) {
            // 取消扫码
            if ([self.ivcsCustomerDelegate respondsToSelector:@selector(onScanCode:withMessage:)]) {
                [self.ivcsCustomerDelegate onScanCode:@NO withMessage:ccsMessage];
            }
        }
    }
    else if([type isEqualToString:SessionMessageTypePeerText]){
        //文本消息
        if ([self.ivcsCustomerDelegate respondsToSelector:@selector(onTextMessage:)]) {
            [self.ivcsCustomerDelegate onTextMessage:ccsMessage];
        }
    }
    else if([type isEqualToString:SessionMessageTypePeerLink]){
        //资源文件消息
        if ([self.ivcsCustomerDelegate respondsToSelector:@selector(onResourceData:)]) {
            ccsMessage.data = [[ZcoModelLinkInfo alloc] initWithDictionary:ccsMessage.data];
            [self.ivcsCustomerDelegate onResourceData:ccsMessage];
        }
    }
    else if([type isEqualToString:SessionMessageTypeCustom]){
        //自定义消息
        if ([self.ivcsCustomerDelegate respondsToSelector:@selector(onCustomMessage:)]) {
            [self.ivcsCustomerDelegate onCustomMessage:ccsMessage];
        }
    }
}

- (NSString *)toJsonString:(NSDictionary *)dict{
    NSError *error;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error:&error];
    NSString *jsonString;
    if (!jsonData) {
        NSLog(@"[文件名：ccs] 转换 json 失败 %@",error);
    } else {
        jsonString = [[NSString alloc]initWithData:jsonData encoding:NSUTF8StringEncoding];
    }
    NSMutableString *mutStr = [NSMutableString stringWithString:jsonString];
    NSRange range = {0,jsonString.length};
    //去掉字符串中的空格
    [mutStr replaceOccurrencesOfString:@" " withString:@"" options:NSLiteralSearch range:range];
    NSRange range2 = {0,mutStr.length};
    //去掉字符串中的换行符
    [mutStr replaceOccurrencesOfString:@"\n" withString:@"" options:NSLiteralSearch range:range2];
    return mutStr;
}

@end
